﻿using System.IO;
using System.Text.RegularExpressions;

namespace NFox.Pycad
{
    public class Project : Package
    {

        public enum ProjectType
        {
            Directory,
            Package,
            Compiled
        }

        public ProjectType Type { get; }

        public override string FullName
        {
            get { return Name; }
        }

        public override bool IsProject { get { return true; } }

        //初始化所有项目
        public Project(string name, DirectoryInfo dir) : base(name)
        {
            Type = ProjectType.Directory;
            _path = dir.FullName;
            foreach (var d in Engine.ProjectPath.GetDirectories())
            {
                if (d.Name.EndsWith(".data")) break;
                Add(new Project(d));
            }
            foreach (var f in Engine.ProjectPath.GetFiles("*.dll"))
            {
                var filename = System.IO.Path.GetFileNameWithoutExtension(f.Name);
                if (!Contains(filename))
                    Add(new Project(f, filename));
            }
        }

        public DirectoryInfo DataDirectory
        {
            get
            {
                if (Type == ProjectType.Package)
                    return Directory.GetDirectory("properties", "data");
                else if (Type == ProjectType.Compiled)
                    return Directory.GetDirectory($"{Name}.data");
                return null;
            }
        }

        public Project(DirectoryInfo dir) : base(dir)
        {
            Type = ProjectType.Package;
            _path = dir.FullName;
            var properties = GetPackage("properties");
            if (properties != null)
            {
                RemovePackage(properties, false);
                foreach (var p in properties.Packages)
                {
                    if (SpecPackage.Names.Contains(p.Name))
                        Add(new SpecPackage(p.Directory));
                }
            }
        }

        private Project(FileInfo file, string name) : base(name)
        {
            Type = ProjectType.Compiled;
            _path = file.FullName;
        }

        public override string Path
        {
            get { return _path; }
        }

        public override DirectoryInfo Directory
        {
            get
            {
                if (Type == ProjectType.Compiled)
                    return new FileInfo(_path).Directory;
                return new DirectoryInfo(_path);
            }
        }

        public override Package CreateSubPackage(string name)
        {
            DirectoryInfo dir;
            FileInfo file;
            switch(Type)
            {
                case ProjectType.Directory:
                    dir = Directory.CreateSubdirectory(name);
                    file = new FileInfo(dir.FullName + "\\" + InitPyFileName);
                    using (var fs = file.Create())
                        fs.Close();
                    var p = new Project(dir);
                    Add(p);
                    return p;
                case ProjectType.Package:
                    if (SpecPackage.Names.Contains(name))
                    {
                        var pdir = Directory.CreateSubdirectory("properties");
                        file = new FileInfo(pdir.FullName + "\\" + InitPyFileName);
                        if(!file.Exists)
                        {
                            using (var fs = file.Create())
                                fs.Close();
                        }
                        dir = pdir.CreateSubdirectory(name);
                        file = new FileInfo(dir.FullName + "\\" + InitPyFileName);
                        using (var fs = file.Create())
                            fs.Close();
                        SpecPackage sp = new SpecPackage(dir);
                        Add(sp);
                        return sp;
                    }
                    break;
            }
            return base.CreateSubPackage(name);
        }

        public string FindFile(string filename)
        {
            return DataDirectory.GetFileFullName(filename);
        }

        public Project GetProject(string name)
        {
            if (name.Contains("."))
                name = name.Substring(0, name.IndexOf('.'));
            foreach (Project p in Packages)
            {
                if (p.Name == name)
                    return p;
            }
            return null;
        }

        public override string ToString()
        {
            return $"Project:({Path})";
        }

    }
}
